%{
AUTHOR: Felipe Arteaga
DATE:  2021-12-15
-------------------------------------------------------------------------
PROJECT: Warnings
-------------------------------------------------------------------------
DESCRIPTION: Main mFile file. Lists of the individual mFiles that generate
all the tables and figures related to Chile. 

REQUISITES:
1) Stata must be installed.
2) rdrobust package must be installed in Stata (run "ssc install rdrobust" on Stata)
3) Latex must be installed

STEPs:

1) Replace 'your_user_name' in line 49 with the user name associated with your session. Hint: you can run this code in Matlab to get it: "char(java.lang.System.getProperty('user.name'))"
2) Modifiy the paths between lines 54 and 65
3) Run this mFile. Good luck.

WARNING:

All figures where exported from a computer with MacOS. They might change in
scale/size/aspect if they are exported from other OS. 


=========================================================================
%}

clc;clear;close all;fclose('all');feature('DefaultCharacterSet','UTF-8');

pcName=char(java.lang.System.getProperty('user.name'));
if(strcmp(pcName,'felipe'))
    % Directories of the project:
    % 1) This is the root folder of the git
    projectDir='/Users/felipe/Dropbox/git/warnings/';
    % 2) This the root folder of the dropbox with no-pii data:
    projectDirData='/Users/felipe/Dropbox/projects/warnings/';

    % 3) Directory of relevant excecutables
    stataExecutablePath='/Applications/Stata/StataMP.app/Contents/MacOS/StataMP';
    latexExecutablePath='/Library/TeX/texbin/pdflatex';

    % 4) Folder to save temporary files. Could be any folder, anywhere, but
    % they must exist:
    stataTempfilesPath='/Users/felipe/Dropbox/myMatlabFunctions/_stataFromMatlab/_tempFiles/';
    latexTempfilesPath='/Users/felipe/Dropbox/myMatlabFunctions/_latexFromMatlab/_tempFiles/';

elseif(strcmp(pcName,'your_pc_name')) % ** CHANGE your_user_name for the actual user name of your sesion (stored in var "pcName")


    % Directories of the project:
    % 1) This is the root folder of the git
    projectDir='/Users/felipe/Dropbox/git/warnings/';
    % 2) This the root folder of the dropbox with no-pii data:
    projectDirData='/Users/felipe/Dropbox/projects/warnings/';

    % 3) Directory of relevant excecutables
    stataExecutablePath='/Applications/Stata/StataMP.app/Contents/MacOS/StataMP';
    latexExecutablePath='/Library/TeX/texbin/pdflatex';

    % 4) Folder to save temporary files. Could be any folder, anywhere, but
    % they must exist:
    stataTempfilesPath='/Users/felipe/Dropbox/myMatlabFunctions/_stataFromMatlab/_tempFiles/';
    latexTempfilesPath='/Users/felipe/Dropbox/myMatlabFunctions/_latexFromMatlab/_tempFiles/';

else
    error('The name "%s" is not in the list.\n Add  "elseif(strcmp(pcName,''%s'')"  and the right paths',pcName,pcName)
end

pathExtraFunctions=[projectDir,'/code/chile/matlabFunctions/'];
fromMainWarningsPaper=true;
assert(exist(pathExtraFunctions,'dir')==7,sprintf('%s path for extra functions does not exist, please add the right one.',pathExtraFunctions))
addpath(genpath(pathExtraFunctions));
cd([projectDir,'code/chile/'])

% Directories related with Latex functions
pathsLatexStruct=struct;
pathsLatexStruct.latexTempfilesPath=latexTempfilesPath;
pathsLatexStruct.latexExecutablePath=latexExecutablePath;
pathsLatexStruct.latexPreTexPath=[pathExtraFunctions,'_latexFromMatlab/preTex.txt'];
pathsLatex(pathsLatexStruct,pathsLatexStruct.latexTempfilesPath);

% Directories related with Stata functions
pathsStataStruct=struct;
pathsStataStruct.stataTempfilesPath=stataTempfilesPath;
pathsStataStruct.stataExecutablePath=stataExecutablePath;
pathsStataStruct.stataProgramsPath=[pathExtraFunctions,'_stataFromMatlab/programs/'];
pathsStataStruct=pathsStata(pathsStataStruct,pathsStataStruct.stataTempfilesPath);

% =========================================================================
%% MAIN BODY TABLES

%% Table 1: Descriptive Statistics for Chilean Choice Applicants
generalStats

%% Table 2: RD Estimates of Platform Pop-Up Effects
% (+ Table B3)
heterFromMain=''; %#ok<NASGU>
analisisRD_Popup
clearvars heterFromMain

%% Table 3: RD Estimates of Platform Pop-Up Effects on Enrolled School Outcomes
analisisRD_Popup_enrolledOutcomes

%% Table 4: WhatsApp RD and RCT Results – 2020
analisisRCT_RD_Whatsapp2020

%% Table 5: RD Estimates of Platform Pop-Up Effects on Subjective Beliefs
analisisRD_Popup_surveyOutcomes

%% Table 6: Estimates of Behavioral Nudge Effects
analisisRCT_SMS_2016

% =========================================================================
%% APPENDIX TABLES

%% Table B1: More Descriptive Statistics for Chilean Choice Applicants
generalStats_moreStats

%% Table B2: Descriptive Statistics for Chilean Choice Applicants– Alternate Samples
generalStats_altSamples

%% Table B3: Platform Pop-Up RD Estimates of Main Outcomes (Table II) with Alternate Bandwidths
% (+ Table 2)
heterFromMain='';  %#ok<NASGU>
analisisRD_Popup
clearvars heterFromMain

%% Table B4: RD Estimates of Platform Pop-Up Effects on Adding Any School, by City and Year
analisisRD_Popup_citiesHeter

%% Table B5: RD Estimates of Platform Pop-Up Effects by Market-Level Choice Experience
heterFromMain='maturity'; %#ok<NASGU>
analisisRD_Popup
clearvars heterFromMain

%% Table B6: RD Estimates of Platform Pop-Up Effects by Applicant's Socioeconomic Status
% CHANGE "heter" from '' to 'vulnerable'
heterFromMain='vulnerable';
analisisRD_Popup
clearvars heterFromMain

%% Table B7: RD Estimates of Platform Pop-Up Effects on Enrolled School Outcomes by Applicant s Socioeconomic Status
analisisRD_Popup_enrolledOutcomesVul

%% Table C2: WhatsApp RD and RCT Results – 2021
analisisRCT_RD_Whatsapp2021

%% Table H1: RD Estimates of Platform Pop-Up Effects
analisisRD_Popup_VAdecomposition

%% Table I1: RD Estimates of Platform Pop-Up Effects - 2017
analisisRD_Popup_2017

%% Table J1: RCT Estimates Split by WhatsApp Message
analisisRCT_RD_Whatsapp2020_arms


% =========================================================================
%% MAIN BODY FIGURES

%% Figures from "generalPlots.m"
% Figure 1a, 1b
% Figures B3b, B3c

generalPlots
%% Figures from "surveyResults.m"
% Figures 2a, 2b, 2c, 3a, 3b, 4a, 4b, 4c
% Figures B3a, B4a to B4e, B5c, B15a

surveyResults
%% Figures from  "analisisRD_Popup.m"
% Figures 5a to 5d
% Figures B5a, B5b, B6a to B6h, B7a to B7l, B8a to B8d

analisisRD_Popup

%% Figures from  "analisisRD_Popup_enrolledOutcomes.m"
% Figures 5e to 5h
% Figures B9a to B9k

analisisRD_Popup_enrolledOutcomes

%% Figures from  "analisisRCT_RD_Whatsapp2020.m"
% Figures 6a to 6e

analisisRCT_RD_Whatsapp2020

%% Figures from  "analisisRCT_RD_Whatsapp2021.m"
% Figures 7
% Figures B13, C9

analisisRCT_RD_Whatsapp2021

%% Figures from  "analisisRD_Popup_surveyOutcomes.m"
% Figures B10a to B10g, B12
analisisRD_Popup_surveyOutcomes

%% Figures from "analisisRD_Popup_citiesHeter.m"
% Figures B11a, B11b
analisisRD_Popup_citiesHeter

%% Figures from "analisisRD_Popup_citiesHeter.m"
% Figures B11c, B11f
analisisRD_Popup_heterTreatmEff

%% Figures from "analisisRD_firstStagePlots.m"
% Figures E1a to E1c, E2a to E2d, E3a to E3d
analisisRD_firstStagePlots

%% Figures from "schoolLevelPlots.m"
% Figures H1a, H1b
schoolLevelPlots

%% Figures form "analisisObligados.m"
% B14
analisisObligados

%%
fprintf('\nTerminooó :)\n\n')

